package o;

import android.os.Build.VERSION;
import android.util.Log;
import androidx.annotation.NonNull;
import com.bumptech.glide.Registry.NoResultEncoderAvailableException;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.Downsampler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

class th<R> implements o.tg.a, Runnable, Comparable<th<?>>, o.aat.d {
    private rl ʻ;
    private b<R> ʻॱ;
    private rh ʼ;
    private Thread ʼॱ;
    private final e ʽ = new e();
    private sa ʽॱ;
    private sa ʾ;
    private boolean ʿ;
    private Object ˈ;
    private rz ˉ;
    private final abb ˊ = abb.ˊ();
    private Object ˊˊ;
    private volatile boolean ˊˋ;
    private tf ˊॱ;
    private sg<?> ˊᐝ;
    private final List<Throwable> ˋ = new ArrayList();
    private volatile tg ˋˊ;
    private int ˋॱ;
    private final o.hg.e<th<?>> ˎ;
    private volatile boolean ˎˎ;
    private final tj<R> ˏ = new tj();
    private int ˏॱ;
    private tn ͺ;
    private final d ॱ;
    private sc ॱˊ;
    private long ॱˋ;
    private int ॱˎ;
    private sa ॱॱ;
    private f ॱᐝ;
    private final a<?> ᐝ = new a();
    private h ᐝॱ;

    static class a<Z> {
        private tr<Z> ˊ;
        private sd<Z> ˋ;
        private sa ॱ;

        a() {
        }

        <X> void ˏ(sa saVar, sd<X> sdVar, tr<X> trVar) {
            this.ॱ = saVar;
            this.ˋ = sdVar;
            this.ˊ = trVar;
        }

        void ˎ(d dVar, sc scVar) {
            aaw.ˋ("DecodeJob.encode");
            try {
                dVar.ˊ().ˊ(this.ॱ, new tc(this.ˋ, this.ˊ, scVar));
            } finally {
                this.ˊ.ˊ();
                aaw.ˏ();
            }
        }

        boolean ॱ() {
            return this.ˊ != null;
        }

        void ˏ() {
            this.ॱ = null;
            this.ˋ = null;
            this.ˊ = null;
        }
    }

    interface b<R> {
        void ˊ(GlideException glideException);

        void ˎ(th<?> thVar);

        void ॱ(tx<R> txVar, rz rzVar);
    }

    final class c<Z> implements d<Z> {
        private final rz ˋ;
        final /* synthetic */ th ˎ;

        c(th thVar, rz rzVar) {
            this.ˎ = thVar;
            this.ˋ = rzVar;
        }

        @NonNull
        public tx<Z> ॱ(@NonNull tx<Z> txVar) {
            return this.ˎ.ˏ(this.ˋ, (tx) txVar);
        }
    }

    interface d {
        uj ˊ();
    }

    static class e {
        private boolean ˋ;
        private boolean ˏ;
        private boolean ॱ;

        e() {
        }

        synchronized boolean ˎ(boolean z) {
            this.ˋ = true;
            return ˊ(z);
        }

        synchronized boolean ॱ() {
            this.ˏ = true;
            return ˊ(false);
        }

        synchronized boolean ˋ() {
            this.ॱ = true;
            return ˊ(false);
        }

        synchronized void ˊ() {
            this.ˏ = false;
            this.ˋ = false;
            this.ॱ = false;
        }

        private boolean ˊ(boolean z) {
            return (this.ॱ || z || this.ˏ) && this.ˋ;
        }
    }

    enum f {
        INITIALIZE,
        SWITCH_TO_SOURCE_SERVICE,
        DECODE_DATA
    }

    enum h {
        INITIALIZE,
        RESOURCE_CACHE,
        DATA_CACHE,
        SOURCE,
        ENCODE,
        FINISHED
    }

    public /* synthetic */ int compareTo(@NonNull Object obj) {
        return ˎ((th) obj);
    }

    th(d dVar, o.hg.e<th<?>> eVar) {
        this.ॱ = dVar;
        this.ˎ = eVar;
    }

    th<R> ˊ(rh rhVar, Object obj, tn tnVar, sa saVar, int i, int i2, Class<?> cls, Class<R> cls2, rl rlVar, tf tfVar, Map<Class<?>, si<?>> map, boolean z, boolean z2, boolean z3, sc scVar, b<R> bVar, int i3) {
        this.ˏ.ˏ(rhVar, obj, saVar, i, i2, tfVar, cls, cls2, rlVar, scVar, map, z, z2, this.ॱ);
        this.ʼ = rhVar;
        this.ॱॱ = saVar;
        this.ʻ = rlVar;
        this.ͺ = tnVar;
        this.ˏॱ = i;
        this.ˋॱ = i2;
        this.ˊॱ = tfVar;
        this.ʿ = z3;
        this.ॱˊ = scVar;
        this.ʻॱ = bVar;
        this.ॱˎ = i3;
        this.ॱᐝ = f.INITIALIZE;
        this.ˈ = obj;
        return this;
    }

    boolean ˎ() {
        h ˏ = ˏ(h.INITIALIZE);
        return ˏ == h.RESOURCE_CACHE || ˏ == h.DATA_CACHE;
    }

    void ˋ(boolean z) {
        if (this.ʽ.ˎ(z)) {
            ᐝ();
        }
    }

    private void ॱ() {
        if (this.ʽ.ॱ()) {
            ᐝ();
        }
    }

    private void ॱॱ() {
        if (this.ʽ.ˋ()) {
            ᐝ();
        }
    }

    private void ᐝ() {
        this.ʽ.ˊ();
        this.ᐝ.ˏ();
        this.ˏ.ˏ();
        this.ˊˋ = false;
        this.ʼ = null;
        this.ॱॱ = null;
        this.ॱˊ = null;
        this.ʻ = null;
        this.ͺ = null;
        this.ʻॱ = null;
        this.ᐝॱ = null;
        this.ˋˊ = null;
        this.ʼॱ = null;
        this.ʽॱ = null;
        this.ˊˊ = null;
        this.ˉ = null;
        this.ˊᐝ = null;
        this.ॱˋ = 0;
        this.ˎˎ = false;
        this.ˈ = null;
        this.ˋ.clear();
        this.ˎ.ˋ(this);
    }

    public int ˎ(@NonNull th<?> thVar) {
        int ʽ = ʽ() - thVar.ʽ();
        if (ʽ == 0) {
            return this.ॱˎ - thVar.ॱˎ;
        }
        return ʽ;
    }

    private int ʽ() {
        return this.ʻ.ordinal();
    }

    public void ˊ() {
        this.ˎˎ = true;
        tg tgVar = this.ˋˊ;
        if (tgVar != null) {
            tgVar.ˏ();
        }
    }

    public void run() {
        aaw.ˏ("DecodeJob#run(model=%s)", this.ˈ);
        sg sgVar = this.ˊᐝ;
        try {
            if (this.ˎˎ) {
                ˋॱ();
                if (sgVar != null) {
                    sgVar.ˏ();
                }
                aaw.ˏ();
                return;
            }
            ʼ();
            if (sgVar != null) {
                sgVar.ˏ();
            }
            aaw.ˏ();
        } catch (tb e) {
            throw e;
        } catch (Throwable th) {
            if (sgVar != null) {
                sgVar.ˏ();
            }
            aaw.ˏ();
        }
    }

    private void ʼ() {
        switch (this.ॱᐝ) {
            case INITIALIZE:
                this.ᐝॱ = ˏ(h.INITIALIZE);
                this.ˋˊ = ʻ();
                ˏॱ();
                return;
            case SWITCH_TO_SOURCE_SERVICE:
                ˏॱ();
                return;
            case DECODE_DATA:
                ͺ();
                return;
            default:
                throw new IllegalStateException("Unrecognized run reason: " + this.ॱᐝ);
        }
    }

    private tg ʻ() {
        switch (this.ᐝॱ) {
            case RESOURCE_CACHE:
                return new ty(this.ˏ, this);
            case DATA_CACHE:
                return new ta(this.ˏ, this);
            case SOURCE:
                return new tw(this.ˏ, this);
            case FINISHED:
                return null;
            default:
                throw new IllegalStateException("Unrecognized stage: " + this.ᐝॱ);
        }
    }

    private void ˏॱ() {
        this.ʼॱ = Thread.currentThread();
        this.ॱˋ = aaq.ॱ();
        boolean z = false;
        while (!this.ˎˎ && this.ˋˊ != null) {
            z = this.ˋˊ.ˎ();
            if (z) {
                break;
            }
            this.ᐝॱ = ˏ(this.ᐝॱ);
            this.ˋˊ = ʻ();
            if (this.ᐝॱ == h.SOURCE) {
                ˋ();
                return;
            }
        }
        if ((this.ᐝॱ == h.FINISHED || this.ˎˎ) && !r0) {
            ˋॱ();
        }
    }

    private void ˋॱ() {
        ॱˊ();
        this.ʻॱ.ˊ(new GlideException("Failed to load resource", new ArrayList(this.ˋ)));
        ॱॱ();
    }

    private void ˊ(tx<R> txVar, rz rzVar) {
        ॱˊ();
        this.ʻॱ.ॱ(txVar, rzVar);
    }

    private void ॱˊ() {
        this.ˊ.ˎ();
        if (this.ˊˋ) {
            throw new IllegalStateException("Already notified", this.ˋ.isEmpty() ? null : (Throwable) this.ˋ.get(this.ˋ.size() - 1));
        }
        this.ˊˋ = true;
    }

    private h ˏ(h hVar) {
        switch (hVar) {
            case RESOURCE_CACHE:
                if (this.ˊॱ.ˋ()) {
                    return h.DATA_CACHE;
                }
                return ˏ(h.DATA_CACHE);
            case DATA_CACHE:
                return this.ʿ ? h.FINISHED : h.SOURCE;
            case SOURCE:
            case FINISHED:
                return h.FINISHED;
            case INITIALIZE:
                if (this.ˊॱ.ˎ()) {
                    return h.RESOURCE_CACHE;
                }
                return ˏ(h.RESOURCE_CACHE);
            default:
                throw new IllegalArgumentException("Unrecognized stage: " + hVar);
        }
    }

    public void ˋ() {
        this.ॱᐝ = f.SWITCH_TO_SOURCE_SERVICE;
        this.ʻॱ.ˎ(this);
    }

    public void ˏ(sa saVar, Object obj, sg<?> sgVar, rz rzVar, sa saVar2) {
        this.ʽॱ = saVar;
        this.ˊˊ = obj;
        this.ˊᐝ = sgVar;
        this.ˉ = rzVar;
        this.ʾ = saVar2;
        if (Thread.currentThread() != this.ʼॱ) {
            this.ॱᐝ = f.DECODE_DATA;
            this.ʻॱ.ˎ(this);
            return;
        }
        aaw.ˋ("DecodeJob.decodeFromRetrievedData");
        try {
            ͺ();
        } finally {
            aaw.ˏ();
        }
    }

    public void ˋ(sa saVar, Exception exception, sg<?> sgVar, rz rzVar) {
        sgVar.ˏ();
        GlideException glideException = new GlideException("Fetching data failed", (Throwable) exception);
        glideException.ˊ(saVar, rzVar, sgVar.ˋ());
        this.ˋ.add(glideException);
        if (Thread.currentThread() != this.ʼॱ) {
            this.ॱᐝ = f.SWITCH_TO_SOURCE_SERVICE;
            this.ʻॱ.ˎ(this);
            return;
        }
        ˏॱ();
    }

    private void ͺ() {
        tx ˎ;
        if (Log.isLoggable("DecodeJob", 2)) {
            ˊ("Retrieved data", this.ॱˋ, "data: " + this.ˊˊ + ", cache key: " + this.ʽॱ + ", fetcher: " + this.ˊᐝ);
        }
        try {
            ˎ = ˎ(this.ˊᐝ, this.ˊˊ, this.ˉ);
        } catch (GlideException e) {
            e.ˏ(this.ʾ, this.ˉ);
            this.ˋ.add(e);
            ˎ = null;
        }
        if (ˎ != null) {
            ˎ(ˎ, this.ˉ);
        } else {
            ˏॱ();
        }
    }

    private void ˎ(tx<R> txVar, rz rzVar) {
        if (txVar instanceof tp) {
            ((tp) txVar).ˊ();
        }
        tr trVar = null;
        if (this.ᐝ.ॱ()) {
            trVar = tr.ˏ(txVar);
            txVar = trVar;
        }
        ˊ(txVar, rzVar);
        this.ᐝॱ = h.ENCODE;
        try {
            if (this.ᐝ.ॱ()) {
                this.ᐝ.ˎ(this.ॱ, this.ॱˊ);
            }
            if (trVar != null) {
                trVar.ˊ();
            }
            ॱ();
        } catch (Throwable th) {
            if (trVar != null) {
                trVar.ˊ();
            }
        }
    }

    private <Data> tx<R> ˎ(sg<?> sgVar, Data data, rz rzVar) throws GlideException {
        if (data == null) {
            sgVar.ˏ();
            return null;
        }
        try {
            long ॱ = aaq.ॱ();
            tx<R> ˏ = ˏ((Object) data, rzVar);
            if (Log.isLoggable("DecodeJob", 2)) {
                ˎ("Decoded result " + ˏ, ॱ);
            }
            sgVar.ˏ();
            return ˏ;
        } catch (Throwable th) {
            sgVar.ˏ();
        }
    }

    private <Data> tx<R> ˏ(Data data, rz rzVar) throws GlideException {
        return ˏ(data, rzVar, this.ˏ.ॱ(data.getClass()));
    }

    @NonNull
    private sc ॱ(rz rzVar) {
        sc scVar = this.ॱˊ;
        if (VERSION.SDK_INT < 26) {
            return scVar;
        }
        boolean z;
        if (rzVar == rz.RESOURCE_DISK_CACHE || this.ˏ.ˋॱ()) {
            z = true;
        } else {
            z = false;
        }
        Boolean bool = (Boolean) scVar.ॱ(Downsampler.ALLOW_HARDWARE_CONFIG);
        if (bool != null && (!bool.booleanValue() || z)) {
            return scVar;
        }
        sc scVar2 = new sc();
        scVar2.ˋ(this.ॱˊ);
        scVar2.ˎ(Downsampler.ALLOW_HARDWARE_CONFIG, Boolean.valueOf(z));
        return scVar2;
    }

    private <Data, ResourceType> tx<R> ˏ(Data data, rz rzVar, ts<Data, ResourceType, R> tsVar) throws GlideException {
        sc ॱ = ॱ(rzVar);
        sl ˏ = this.ʼ.ॱ().ˏ(data);
        try {
            tx<R> ˏ2 = tsVar.ˏ(ˏ, ॱ, this.ˏॱ, this.ˋॱ, new c(this, rzVar));
            return ˏ2;
        } finally {
            ˏ.ˊ();
        }
    }

    private void ˎ(String str, long j) {
        ˊ(str, j, null);
    }

    private void ˊ(String str, long j, String str2) {
        Log.v("DecodeJob", str + " in " + aaq.ˎ(j) + ", load key: " + this.ͺ + (str2 != null ? ", " + str2 : "") + ", thread: " + Thread.currentThread().getName());
    }

    @NonNull
    public abb ˏ() {
        return this.ˊ;
    }

    @NonNull
    <Z> tx<Z> ˏ(rz rzVar, @NonNull tx<Z> txVar) {
        si ˋ;
        rw ˊ;
        sd sdVar;
        Class cls = txVar.ˋ().getClass();
        if (rzVar != rz.RESOURCE_DISK_CACHE) {
            ˋ = this.ˏ.ˋ(cls);
            tx ˏ = ˋ.ˏ(this.ʼ, txVar, this.ˏॱ, this.ˋॱ);
        } else {
            tx<Z> txVar2 = txVar;
            ˋ = null;
        }
        if (!txVar.equals(ˏ)) {
            txVar.ॱॱ();
        }
        if (this.ˏ.ˎ(ˏ)) {
            sd ˏ2 = this.ˏ.ˏ(ˏ);
            ˊ = ˏ2.ˊ(this.ॱˊ);
            sdVar = ˏ2;
        } else {
            ˊ = rw.NONE;
            sdVar = null;
        }
        if (!this.ˊॱ.ˊ(!this.ˏ.ˏ(this.ʽॱ), rzVar, ˊ)) {
            return ˏ;
        }
        if (sdVar == null) {
            throw new NoResultEncoderAvailableException(ˏ.ˋ().getClass());
        }
        sa teVar;
        switch (ˊ) {
            case SOURCE:
                teVar = new te(this.ʽॱ, this.ॱॱ);
                break;
            case TRANSFORMED:
                teVar = new tu(this.ˏ.ॱॱ(), this.ʽॱ, this.ॱॱ, this.ˏॱ, this.ˋॱ, ˋ, cls, this.ॱˊ);
                break;
            default:
                throw new IllegalArgumentException("Unknown strategy: " + ˊ);
        }
        ˏ = tr.ˏ(ˏ);
        this.ᐝ.ˏ(teVar, sdVar, ˏ);
        return ˏ;
    }
}
